package com.frxuexi.user.view

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.AskLeave
import com.frxuexi.user.intent.AskIntent
import com.frxuexi.user.viewmodel.AskViewModel
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

/**
 * 请假页面
 */
@Composable
fun AskLeavePage(navHostController: NavHostController,vm:AskViewModel= hiltViewModel()) {
    val kv = MMKV.defaultMMKV()

    val name by remember {
        mutableStateOf(kv.decodeString("name", "未登录"))
    }
    val role by remember {
        mutableStateOf(kv.decodeInt("role", 0))
    }

    var dateStart by remember {
        mutableStateOf(SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()))
    }
    var dateEnd by remember {
        mutableStateOf(SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()))
    }

    var type by remember {
        mutableStateOf("事假")
    }

    var method by remember {
        mutableStateOf("按天")
    }

    var reason by remember {
        mutableStateOf("")
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        Log.i("===","请假成功${it.data}")
                        navHostController.popBackStack()
                    }
                    else -> {}
                }
            }
        }
    })

    Column {
        MyTitleBar(title = "请假申请", navHostController = navHostController)
        Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
            ElevatedCard(
                modifier = Modifier.fillMaxWidth(),
                colors = CardDefaults.elevatedCardColors(containerColor = Color.White)
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(10.dp)
                ) {
                    AsyncImage(
                        model = "https://img1.baidu.com/it/u=3406115504,1068597125&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1725037200&t=3c5e09c88f402ee3f67ff412d5dae000",
                        contentDescription = "",
                        modifier = Modifier
                            .size(80.dp)
                            .clip(CircleShape),
                        contentScale = ContentScale.Crop
                    )
                    Spacer(modifier = Modifier.width(10.dp))
                    Column(
                        modifier = Modifier.height(80.dp),
                        verticalArrangement = Arrangement.SpaceAround
                    ) {
                        Text(text = name!!)
                        Text(text = if (role == 1) "经理" else if (role == 2) "产品经理" else if (role == 3) "员工" else "")
                    }
                }
            }

            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "请假类型：")
//                OutlinedTextField(value = type, onValueChange = {type=it})
                RadioButton(selected = type=="事假", onClick = {
                    type="事假"
                })
                Text(text = "事假")

                RadioButton(selected = type=="病假", onClick = {
                    type="病假"
                })
                Text(text = "病假")
            }
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "请假方式：")
                RadioButton(selected = method=="按天", onClick = {
                    method="按天"
                })
                Text(text = "按天")

                RadioButton(selected = method=="按小时", onClick = {
                    method="按小时"
                })
                Text(text = "按小时")
            }
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "开始时间：")
                OutlinedTextField(value = dateStart, onValueChange = {dateStart=it})
            }
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "结束时间：")
                OutlinedTextField(value = dateEnd, onValueChange = {dateEnd=it})
            }
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "请假原因：")
                OutlinedTextField(value = reason, onValueChange = {reason=it})
            }
            Button(modifier = Modifier.fillMaxWidth(),onClick = {

                val uid = kv.decodeInt("uid", 0)
                val askLeave = AskLeave(
                    null,
                    uid,
                    name,
                    type,
                    method,
                    reason,
                    dateStart,
                    dateEnd,
                    System.currentTimeMillis(),
                    0,
                    "已提交"
                )
                vm.sendIntent(AskIntent.commitAsk(askLeave))
            }) {
                Text(text = "提交")
            }
        }
    }
}